package collections.map;

import com.google.common.collect.Collections2;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.junit.Test;

import java.util.*;

/**
 * @Author: ycy
 * @CreateDate: 2019/4/25 17:23
 */
public class MapDemo {

  @Test
  public void test() {
    final Map<String, Entity> map = new HashMap<>();
    for (int i = 0; i < 13; i++) {
      Entity entity = new Entity();
      System.out.println(i);
      String key = i + "大头";
      map.put(key, entity);
    }
    map.put("helloword", new Entity());
  }

  @Test
  public void test1() {
    //天然红黑树,如果你想让key按照这个玩意排序 那就用这个
    Map<String, Entity> map = new TreeMap<>();

    for (int i = 0; i < 13; i++) {
      Entity entity = new Entity();
      map.put(i + "大头", entity);
    }

    for (Map.Entry<String, Entity> stringEntityEntry : map.entrySet()) {
      System.out.println(stringEntityEntry.getValue());
    }

  }

  @Test
  public void test2() {
    //天然红黑树,如果你想让key按照这个玩意排序 那就用这个
    Map<String, Entity> map = new LinkedHashMap(12, 0.75f, true) {
      int size = 5;

      @Override
      protected boolean removeEldestEntry(Map.Entry eldest) {
        return size() > size;
      }
    };
    for (int i = 0; i < 13; i++) {
      Entity entity = new Entity();
      map.put(i + "大头", entity);
    }
    for (Map.Entry<String, Entity> stringEntityEntry : map.entrySet()) {
      System.out.println(stringEntityEntry);
    }

    //访问一次,那么这个key的顺序就放到了链表中的第一个位置
    System.out.println(map.get("10大头"));
    for (Map.Entry<String, Entity> stringEntityEntry : map.entrySet()) {
      System.out.println(stringEntityEntry);
    }

  }

  class Entity {
    Long id;
    Float price;

    public Entity() {
    }


  }


}


